package com.eduction.resource;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;

import com.eduction.common.MessageContants;
import com.eduction.common.ResultBean;

import net.sf.json.JSONObject;

@Controller
public class BaseController {
	public Object getSessionUser(String sessionName,HttpServletRequest request){
		Object object = request.getSession().getAttribute(sessionName);
		ResultBean resultBean = new ResultBean();
		if (null==object) {
			resultBean.setCode(ResultBean.ERRORCODE);
			resultBean.setMessage(MessageContants.SESSION_ERROR);
			return resultBean;
		}
		return object  ;
	}
	public  static ResponseEntity<String> toResponse(Object o,String code,String errorMsg){
			
			if(null==o){
				o="0";
			}
			if(null==code){
				code="000000";
			}
			if(null==errorMsg){
				errorMsg="0";
			}
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("code", code);
			map.put("errorMsg", errorMsg);
			map.put("data", o);
			JSONObject jsonObject = JSONObject.fromObject(map);
			return new ResponseEntity<String>(jsonObject.toString(),HttpStatus.OK);
			
	}
}
